package com.dys.testjdk15;

/**
 * @className: com.dys.testjdk15.Test
 * @description: 密封类
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/09 下午8:51
 */
public class TestSealedClass {
}

/**
 * sealed 对 Person 类进行密封
 * permits 指明哪些类可以继承
 * 子类必须是 final 修饰的或者也是密封的
 * 如果子类不想被密封,可以使用 non-sealed 修饰
 */
sealed class Person permits Worker, Teacher, Cook, Boss, Employee, Student {
}

final class Cook extends Person {
}

final class Boss extends Person {
}

final class Employee extends Person {
}

final class Teacher extends Person {
}

// 密封的子类允许继续有子类
sealed class Student extends Person permits PrimaryStudent, GraduateStudent {
}

final class PrimaryStudent extends Student {
}

final class GraduateStudent extends Student {
}

// 通过non-sealed取消子类密封
non-sealed class Worker extends Person {
}

class CarWorker extends Worker {
}